//
//  MCAddGoodsCategoryViewController.swift
//  StoreManage
//
//  Created by MC on 2018/9/11.
//  Copyright © 2018年 MC. All rights reserved.
//

import UIKit

class MCAddGoodsCategoryViewController: MCViewController {
    
    public var isNewGoodsCategory = true
    public var model : MCGoodsCategoryModel?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        baseSetting()
        
        initUI()
    }
    
    // MARK: - Setter & Getter
    lazy var categoryNameView: MCInputBarView = {
        let view = MCInputBarView()
        view.leftLabel.text = "分类名称"
        return view
    }()
    
    lazy var switchView: MCSwitchBarView = {
        let view = MCSwitchBarView()
        view.leftLabel.text = "是否显示在小程序"
        view.rightSwitch.isOn = true
        return view
    }()
    
    lazy var bottomButton: UIButton = {
        let button = UIButton.init(type: UIButtonType.custom)
        button.titleLabel?.font = MCFont(14)
        button.setTitleColor(UIColor.white, for: .normal)
        button.setTitle("完成", for: .normal)
        button.backgroundColor = UIColor.MCMain
        button.addTarget(self, action: #selector(bottomButtonClicked), for: .touchUpInside)
        return button
    }()

}

//MARK: 通知回调，闭包回调，点击事件
extension MCAddGoodsCategoryViewController {
   
    @objc func rightItemClicked() {
        
        UIAlertController.showConfirmOn(self,title: "确认删除", message: "删除该分类后其下属商品将全部移至“其他”分类", confirm: { (_) in
            self.sendNetworking_delete()
        })
    }
    
    @objc func bottomButtonClicked() {
        sendNetworking_updateOrAdd()
    }
}


//MARK: 网络请求
extension MCAddGoodsCategoryViewController {
    
    func sendNetworking_updateOrAdd() {
        
        
        let name = categoryNameView.rightTextField.text ?? ""
        
        if name.count > 4 {
            MCHUD_text(message: "分类名称不可多于四个字", view: view, delay: 1.2)
            return
        }
        
        
        let isShowWx = switchView.rightSwitch.isOn ? 1 : 0
        
        
        let para = [
            "isShowWx" : isShowWx,
            "name"     : name,
            "selfCategoryId" : model?.selfCategoryId.intValue ?? 0
            ] as [String : Any]

        let urlStr = isNewGoodsCategory ? JAVA_addCategory : JAVA_updateCategory
        
        MCNetwork.POST(urlStr, para, vc: self, success: { (data) in
        
            let message = self.isNewGoodsCategory ? "新增分类成功" : "编辑分类成功"
            
            MCHUD_text(message: message, view: self.view, delay: 1.0)
            
            DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1.0, execute: {
                self.navigationController?.popViewController(animated: true)
            })
            
        }, failure: nil)
    }
    
    func sendNetworking_delete() {
        let para = [
            "selfCategoryId" : model?.selfCategoryId.intValue ?? 0
            ] as [String : Any]

        MCNetwork.POST(JAVA_deleteCategory, para, vc: self, success: { (data) in

            MCHUD_text(message: "删除分类成功", view: self.view, delay: 1.0)

            DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1.0, execute: {
                self.navigationController?.popViewController(animated: true)
            })

        }, failure: nil)
    }
}

//MARK: UI的处理,通知的接收
extension MCAddGoodsCategoryViewController {
    
    func baseSetting() {
        
        self.title = isNewGoodsCategory ? "添加分类" : "编辑分类"
        
        navigationItem.rightBarButtonItem = isNewGoodsCategory ? nil : MCSetBarButtonItem_text("删除", target: self, selector: #selector(rightItemClicked))
        
        categoryNameView.rightTextField.text = model?.name
        if model?.isShowWx == 0 {
            switchView.rightSwitch.isOn = false
        } else {
            switchView.rightSwitch.isOn = true
        }
        
    }
    
    
    func initUI() {
        view.addSubview(categoryNameView)
        categoryNameView.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(15)
            make.right.equalTo(-15)
            make.top.equalTo(10)
            make.height.equalTo(50)
        }
        
        view.addSubview(switchView)
        switchView.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(15)
            make.right.equalTo(-15)
            make.top.equalTo(categoryNameView.snp.bottom).offset(10)
            make.height.equalTo(50)
        }
        
        view.addSubview(bottomButton)
        bottomButton.snp.remakeConstraints { (make) ->Void in
            make.left.right.bottom.equalTo(view)
            make.height.equalTo(44)
        }
    }
}
